package com.aisile.common.yongyou;

import com.aisile.common.exception.CustomExceptionThrows;
import com.aisile.model.common.enums.AdminHttpCodeEnum;
import com.alibaba.fastjson.JSON;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther:yry
 * @Date:2024/4/28 0028
 * @VERSON:1.0
 */
@Setter
@Component
@PropertySource("classpath:yongyou.properties")
@ConfigurationProperties(prefix = "yongyou")
public class YongYouCommonUtils {

    private String idcardocrcode;
    private String matchidentitycode;
    private String biologicalexaminationcode;
    private String personandcertificateverificationcode;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * OCR 仅支持URL方式校验
     */
    public Map IdCardOcr(String image) {
//        Map data  = responseEntity.getBody().get(data);
//        if(){
//            // 判断逻辑
//        }
//        return data;
        return new HashMap();
    }


    /**
     * 二要素 结果判断 入参和出参
     */
    public boolean matchIdentity(String username, String idNumber) {
        // 防止不正当调用接口 损失金钱
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(idNumber)) {
            return false;
        }
        // String method = "POST";
        String url = "https://api.yonyoucloud.com/apis/dst/matchIdentity/matchIdentity";

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("apicode", matchidentitycode);//APICODE
        httpHeaders.add("Content-Type", "application/json");


        Map<String, Object> params = new HashMap<String, Object>();//请求参数
        params.put("idNumber", idNumber);
        params.put("userName", username);

        HttpEntity<String> httpEntity = new HttpEntity<>(JSON.toJSONString(params), httpHeaders);

        // 400100 证明二要素检验一致
        // <200,{success=false, code=400432, message=身份证号码不对, data=null},[Date:"Sun, 28 Apr 2024 06:21:57 GMT", Content-Type:"application/json;charset=UTF-8", Transfer-Encoding:"chunked", Connection:"keep-alive", Set-Cookie:"acw_tc=2760829c17142853171574779e3e8fa43b40989bdba6479daa7ff275cb6e62;path=/;HttpOnly;Max-Age=1800", Vary:"Accept-Encoding", X-Ymc-Access-Data:"055222049044047048046033029041027029032032046222246222237243238234238237234237239245234237236236246244236244236222232222037042035046033047047027042029041033222246222044044029236244052244053222232222046033045049033047048027048037041033222246222236234236242236222232222047048029048049047222246238236236232222042029041033047044029031033222246222031244243033238238242243233237236236237233240031243236233030030238029233029030240237034239030244237029029239222232222048046029031033005032222246222033244245030030238238031241239236236236031241034222232222048037041033222246222238244024235253044046024235238236238240246236242246238237246241243220231236236236236222232222046033041043048033027029032032046222246222241245234237236244234243238234237237222232222046033045049033047048027040033042035048036222246238245237244057", Access-Control-Expose-Headers:"X-Ymc-Access-Data"]>
        //ResponseEntity<Map> responseEntity =  restTemplate.postForEntity(url,httpEntity,Map.class);
//        System.out.println(responseEntity);
//        System.out.println(responseEntity.getBody());
//        System.out.println(responseEntity.getBody().get("message"));
        int code = 400100;  //400432 400100
        //return (((Integer)responseEntity.getBody().get("code")) == 400100);

        return code == 400100;

    }


    public int biologicalexamination(String image, int type) {
        // 采用计数法 区分三种不同状态  0 成功  正整数 疑似  负数 失败
        int i = 0;
        // String method = "POST";
        String url = "https://api.yonyoucloud.com/apis/dst/Biologicalexamination/Biologicalexamination";

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("apicode", biologicalexaminationcode);//APICODE
        httpHeaders.add("Content-Type", "application/json");


        Map<String, Object> params = new HashMap<String, Object>();//请求参数
        params.put("image", image);
        params.put("imageType", "URL");

        HttpEntity<String> httpEntity = new HttpEntity<>(JSON.toJSONString(params), httpHeaders);

       // ResponseEntity<Map> responseEntity = restTemplate.postForEntity(url, httpEntity, Map.class);
        //System.out.println(responseEntity);
        //System.out.println(responseEntity.getBody());
        //System.out.println(responseEntity.getBody().get("message"));

        String success = "{\"message\":\"成功\",\"data\":{\"checkStatus\":\"0\",\"score\":\"95\",\"tradeNo\":\"1234183059902451712\",\"remark\":\"检测成功\",\"code\":\"0\"},\"code\":\"601200000\"}";
        String yisi = "{\"message\":\"成功\",\"data\":{\"checkStatus\":\"0\",\"score\":\"84\",\"tradeNo\":\"1234183059902451712\",\"remark\":\"检测成功\",\"code\":\"0\"},\"code\":\"601200000\"}";
        String fail = "{\"message\":\"成功\",\"data\":{\"checkStatus\":\"0\",\"score\":\"60\",\"tradeNo\":\"1234183059902451712\",\"remark\":\"检测成功\",\"code\":\"0\"},\"code\":\"601200000\"}";

        Map map = null;
        if (type == 1) {
            map = JSON.parseObject(success, Map.class);
        } else if (type == 2) {
            map = JSON.parseObject(yisi, Map.class);
        } else {
            map = JSON.parseObject(fail, Map.class);
        }
        // score >= 90 活人   score <= 89 >= 70 疑似  score <=69 失败
        if (map.get("code").equals("601200000")) {

            Map datas = (Map) map.get("data");
            Integer score = Integer.valueOf((String) datas.get("score"));
            if (score <= 69 && score >= 0) {
                i--;
            } else if (score <= 89 && score >= 70) {
                i++;
            }
        } else {
            i--;
        }
        return i;
    }


}
